//
//  XGCellFrame.m
//  XGSeeDetails
//
//  Created by 小果 on 16/8/29.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGCellFrame.h"
#import "XGCellModel.h"
@implementation XGCellFrame
-(void)setCellModel:(XGCellModel *)cellModel{
    _cellModel = cellModel;
    
    CGFloat iconX = 15;
    CGFloat iconY = 10;
    CGFloat iconW = 50;
    CGFloat iconH = 50;
    self.iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    CGSize nameSize = [cellModel.name sizeWithFont:[UIFont boldSystemFontOfSize:18] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameX = CGRectGetMaxX(self.iconF)+10;
    CGFloat nameY = iconY;
    self.nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    
    CGSize introSize;
    if(self.isOpened == YES){
        introSize = [cellModel.intro sizeWithFont:[UIFont boldSystemFontOfSize:16] maxSize:CGSizeMake(ScreenW -nameX - 10, MAXFLOAT)];
    }else{
        introSize = [cellModel.intro sizeWithFont:[UIFont boldSystemFontOfSize:16] maxSize:CGSizeMake(ScreenW -nameX - 10, 10)];
    }
    CGFloat introX = nameX;
    CGFloat introY = CGRectGetMaxY(self.nameF) +10;
    self.introF = CGRectMake(introX, introY, introSize.width, introSize.height);
    
    self.cellHeight = CGRectGetMaxY(self.introF)+10;
    
    CGFloat sepX = nameX;
    CGFloat sepH = 1.0;
    CGFloat sepY = self.cellHeight;
    CGFloat sepW = ScreenW - sepX;
    self.sepLineF = CGRectMake(sepX, sepY, sepW, sepH);
    
    CGFloat seeW = 23;
    CGFloat seeH = 23;
    CGFloat seeX = ScreenW - 33;
    CGFloat seeY = iconY;
    self.seeF = CGRectMake(seeX, seeY, seeW, seeH);
    
    
}

+(NSMutableArray *)cellSourceWithName:(NSString *)sourceName{
    NSString *path = [[NSBundle mainBundle] pathForResource:sourceName ofType:@"json"];
    NSData *dataArray = [NSData dataWithContentsOfFile:path];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:dataArray options:0 error:nil];
    NSMutableArray *endArray = [NSMutableArray array];
    
    [array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        XGCellModel *model = [XGCellModel cellModelWithDict:dict];
        XGCellFrame *cellFrame = [[XGCellFrame alloc] init];
        cellFrame.cellModel = model;
        [endArray addObject:cellFrame];
    }];
    
    return endArray;
}

@end
